package me.idashu.attg.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import me.idashu.attg.domain.AdverEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.domain.ShopEntity;
import me.idashu.attg.service.AdverService;
import me.idashu.attg.service.ShopService;
import me.idashu.attg.util.Escape;
import me.idashu.attg.util.SystemConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/shop")
public class ShopController {
	@Autowired
	private ShopService shopService;

	@Autowired
	private AdverService adverService;

	@Autowired
	private SystemConfig systemConfig;

	@RequestMapping("/addShop")
	public String setCirl(Model model) {
		model.addAttribute("addShop", "添加商家");
		return "add_shop";
	}

	@RequestMapping("/addShopInfo")
	public String addCirlInfo(@ModelAttribute ShopEntity shopEntity,
			@RequestParam(value = "file", required = false) MultipartFile file,
			Model model, HttpServletRequest request) {
		try {
			String dir = systemConfig.getDir("dir");
			String prodir = systemConfig.getDir("prodir");
			System.out.println("file------->:" + file);
			if (file.getOriginalFilename() != null
					&& !"".equals(file.getOriginalFilename())) {
				System.out.println("file:" + file.getOriginalFilename());
				try {
					InputStream is = file.getInputStream();
					byte[] inOutb = new byte[is.available()];
					String uuid = UUID.randomUUID().toString().replace("-", "");
					FileOutputStream fos = new FileOutputStream(new File(dir
							+ uuid + ".jpg"));
					shopEntity.setLogo(prodir + uuid + ".jpg");
					is.read(inOutb);
					fos.write(inOutb);
					is.close();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			shopEntity.setTime(new Date());
			shopEntity.setFlag("Y");
			shopService.insertShop(shopEntity);
			model.addAttribute("success", "添加商家成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	@RequestMapping("/getShops")
	public String getShops(Model model) {
		model.addAttribute("getShops", "添加商家");
		ShopEntity shopEntity = new ShopEntity();
		PageEntity pageEntity = new PageEntity();
		selectShops(model, shopEntity, pageEntity);
		return "shop_list";
	}

	@RequestMapping("/shopAjaxList")
	public String shopAjaxList(Model model,
			@ModelAttribute ShopEntity shopEntity,
			@ModelAttribute PageEntity pageEntity) {
		if (shopEntity.getName() != null && !"".equals(shopEntity.getName())) {
			String title = Escape.unescape(shopEntity.getName());
			shopEntity.setName(title);
		}
		selectShops(model, shopEntity, pageEntity);
		return "shopAjaxList";
	}

	@RequestMapping("/selectOneShop")
	public String selectOneShop(Model model, @RequestParam("id") long id) {
		ShopEntity shop = shopService.findShopById(id);
		model.addAttribute("shop", shop);
		return "shop_info";
	}

	private void selectShops(Model model, ShopEntity shopEntity,
			PageEntity pageEntity) {
		int total = shopService.findCountShopsByPage(shopEntity);
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageEntity", pageEntity);
		map.put("shopEntity", shopEntity);
		List<ShopEntity> shops = shopService.findShopsByPage(map);
		if (shops.size() > 0) {
			model.addAttribute("shops", shops);
		} else {
			model.addAttribute("shops", null);
		}
		model.addAttribute("pageEntity", pageEntity);
	}

	@RequestMapping("/deleteShop")
	public String deleteShop(Model model,
			@ModelAttribute ShopEntity shopEntity,
			@ModelAttribute PageEntity pageEntity) {
		shopService.deleteShop(shopEntity.getId());
		selectShops(model, shopEntity, pageEntity);
		return "shopAjaxList";
	}

	@RequestMapping("/addAdver")
	public String addAdver(Model model, @ModelAttribute ShopEntity shopEntity) {
		model.addAttribute("addAdver", "添加招聘信息");
		ShopEntity shop = shopService.findShopById(shopEntity.getId());
		model.addAttribute("shopEntity", shop);
		return "addAdver";
	}

	@RequestMapping("/addAdverInfo")
	public String addAdverInfo(Model model,
			@ModelAttribute AdverEntity adverEntity) {
		adverEntity.setTime(new Date());
		adverService.insertAdver(adverEntity);
		model.addAttribute("success", "添加招聘信息成功！");
		return "success";
	}

	@RequestMapping("/getAdversByShopId")
	public String getAdversByShopId(Model model,
			@ModelAttribute AdverEntity adverEntity,
			@ModelAttribute PageEntity pageEntity) {
		int total = adverService.findCountAdversByShopIdPage(adverEntity);
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageEntity", pageEntity);
		map.put("adverEntity", adverEntity);
		List<AdverEntity> advers = adverService.findAdversByShopIdPage(map);
		if (advers.size() > 0) {
			model.addAttribute("advers", advers);
		} else {
			model.addAttribute("advers", null);
		}
		model.addAttribute("pageEntity", pageEntity);
		model.addAttribute("shopId", adverEntity.getShop_id());
		return "adverslist";
	}
	
	@RequestMapping("/deleteAdver")
	public String deleteAdver(Model model,
			@ModelAttribute AdverEntity adverEntity,
			@ModelAttribute PageEntity pageEntity) {
		adverService.deleteAdver(adverEntity.getId());
		int total = adverService.findCountAdversByShopIdPage(adverEntity);
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageEntity", pageEntity);
		map.put("adverEntity", adverEntity);
		List<AdverEntity> advers = adverService.findAdversByShopIdPage(map);
		if (advers.size() > 0) {
			model.addAttribute("advers", advers);
		} else {
			model.addAttribute("advers", null);
		}
		model.addAttribute("pageEntity", pageEntity);
		model.addAttribute("shopId", adverEntity.getShop_id());
		return "adverslist";
	}
	
	@RequestMapping("/updateShopInfo")
	public String updateShopInfo(Model model, @ModelAttribute ShopEntity shopEntity) {
		model.addAttribute("updateShop", "修改商家信息");
		ShopEntity shop = shopService.findShopById(shopEntity.getId());
		model.addAttribute("shopEntity", shop);
		return "update_shop";
	}
	
	@RequestMapping("/updateShop")
	public String updateShop(@ModelAttribute ShopEntity shopEntity,
			@RequestParam(value = "file", required = false) MultipartFile file,
			Model model, HttpServletRequest request) {
		try {
			String dir = systemConfig.getDir("dir");
			String prodir = systemConfig.getDir("prodir");
			System.out.println("file------->:" + file);
			if (file.getOriginalFilename() != null
					&& !"".equals(file.getOriginalFilename())) {
				System.out.println("file:" + file.getOriginalFilename());
				try {
					InputStream is = file.getInputStream();
					byte[] inOutb = new byte[is.available()];
					String uuid = UUID.randomUUID().toString().replace("-", "");
					FileOutputStream fos = new FileOutputStream(new File(dir
							+ uuid + ".jpg"));
					shopEntity.setLogo(prodir + uuid + ".jpg");
					is.read(inOutb);
					fos.write(inOutb);
					is.close();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			shopService.updateShop(shopEntity);
			model.addAttribute("success", "修改商家成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

}
